//
//  YXScrollowActionSheet.m
//  YXCustomActionSheet
//
//  Created by Houhua Yan on 16/7/14.
//  Copyright © 2016年 YanHouhua. All rights reserved.
//

#import "YXScrollowActionSheet.h"
#import "WXApi.h"
#import <TencentOpenAPI/QQApiInterface.h>
#import "WeiboSDK.h"

@interface YXScrollowActionSheet()
{
    UIView *_backView;
}

@end

@implementation YXScrollowActionSheet

/**展示*/
- (void)show
{
    [self showWithContentArray:@[] blockArr:@[]];
}

- (void)showWithContentArray:(NSArray<NSString *> *)btnStrArray blockArr:(NSArray<void (^)()> *)blockArr
{
    __weak typeof(self) weakSelf = self;
//    NSString *cancelBtn = @"取消";
    NSString *cancelBtn = NSLocalizedString(@"Cancel", nil);
    NoParaNoReturnBlock cancelBlock = ^() {
        [weakSelf tapBg];
    };
    
    NSMutableArray *btnStrMuArray = [NSMutableArray arrayWithArray:btnStrArray];
    [btnStrMuArray addObject:cancelBtn];
    
    NSMutableArray *blockMuArr = [NSMutableArray arrayWithArray:blockArr];
    [blockMuArr addObject:cancelBlock];
    
    self.backgroundColor = RGB(249, 249, 249);
    [self addBackView:kKeyWindow];
    
    NSMutableArray *contentArray = [NSMutableArray arrayWithCapacity:10];
    if ([WeiboSDK isWeiboAppInstalled]) {
//        [contentArray addObject:@{@"name":@"新浪微博",@"icon":@"sns_icon_3",@"tag":@"0"}];
        [contentArray addObject:@{@"name":NSLocalizedString(@"Weibo",nil),@"icon":@"sns_icon_3",@"tag":@"0"}];
    }
    
    if ([QQApiInterface isQQInstalled]) {
        [contentArray addObject:@{@"name":@"QQ ",@"icon":@"sns_icon_4",@"tag":@"4"}];
//        [contentArray addObject:@{@"name":@"QQ空间 ",@"icon":@"sns_icon_5",@"tag":@"5"}];
        [contentArray addObject:@{@"name":NSLocalizedString(@"QZone", nil),@"icon":@"sns_icon_5",@"tag":@"5"}];
    }
    
    if ([WXApi isWXAppInstalled]) {
//        [contentArray addObject:@{@"name":@"微信",@"icon":@"sns_icon_7",@"tag":@"1"}];
        [contentArray addObject:@{@"name":NSLocalizedString(@"WeChat", nil),@"icon":@"sns_icon_7",@"tag":@"1"}];
//        [contentArray addObject:@{@"name":@"朋友圈",@"icon":@"sns_icon_8",@"tag":@"2"}];
        [contentArray addObject:@{@"name":NSLocalizedString(@"Moments", nil),@"icon":@"sns_icon_8",@"tag":@"2"}];
    }
    
    CGFloat hei = 0;
    if (contentArray.count > 0 && !self.shareModel.notallowShare) {
        //分享到
        UILabel *titleLabel = [[UILabel alloc] init];
        titleLabel.text = NSLocalizedString(@"Share:", nil);

        titleLabel.backgroundColor = [UIColor clearColor];
        titleLabel.font = [UIFont boldSystemFontOfSize:17];
        titleLabel.textAlignment = NSTextAlignmentCenter;
        titleLabel.textColor = RGB(81, 95, 108);
        titleLabel.frame = CGRectMake(0, 10, self.width, 30);
        [self addSubview:titleLabel];
        
        CGFloat btnW = 60;
        CGFloat btnH = 60;
        CGFloat marginX = 15;
        CGFloat firstY = 50;
        
        UIScrollView *scrollowView = [[UIScrollView alloc] init];
        scrollowView.showsHorizontalScrollIndicator = NO;
        scrollowView.frame = CGRectMake(0, firstY, self.width, 80);
        for (int i = 0; i < contentArray.count; i++)
        {
            NSDictionary *dic = [contentArray objectAtIndex:i];
            NSString *name = dic[@"name"];
            NSString *icon = dic[@"icon"];
            NSInteger tag = [dic[@"tag"] integerValue];
            YXActionSheetButton *btn = [YXActionSheetButton buttonWithType:UIButtonTypeCustom];
            btn.tag = tag;
            [btn setTitle:name forState:UIControlStateNormal];
            [btn setImage:[UIImage imageNamed:icon] forState:UIControlStateNormal];
            __weak typeof(btn) tmpBtn = btn;
            [btn setTapActionWithBlock:^{
                [weakSelf tapBg];
                NSInteger tag = tmpBtn.tag;
                UMSocialPlatformType type = (UMSocialPlatformType)tag;
                [weakSelf.shareModel shareWebPageToPlatformType:type];
            }];
            
            CGFloat btnX = marginX + (marginX + btnW) * i;
            CGFloat btnY = 5;
            btn.frame = CGRectMake(btnX, btnY, btnW, btnH);
            
            [scrollowView addSubview:btn];
        }
        scrollowView.contentSize = CGSizeMake(marginX +( marginX + btnW )* contentArray.count, 0);
        [self addSubview:scrollowView];
        
        //分割线
        UIView *line = [[UIView alloc] init];
        line.frame = CGRectMake(40, scrollowView.bottom, KSCREENWIDTH-80, 0.5);
        line.backgroundColor = [UIColor c5Color];
        [self addSubview:line];
        hei = scrollowView.bottom;
    }

    for (int i = 0; i < btnStrMuArray.count; i++)
    {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn setTapActionWithBlock:^{
            NoParaNoReturnBlock block = blockMuArr[i];
            if (block) {
                block();
                [weakSelf tapBg];
            }
        }];
        
        btn.titleLabel.font = [UIFont boldSystemFontOfSize:17];
        [btn setTitle:btnStrMuArray[i] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor c5Color] forState:UIControlStateNormal];
        btn.layer.borderColor = [UIColor c5Color].CGColor;
        btn.layer.borderWidth = .5;
        btn.layer.cornerRadius = 5;
        btn.clipsToBounds = YES;
        btn.hidden = NO;
        btn.frame = CGRectMake(20, hei + 5, KSCREENWIDTH - 40, 40);
        hei += 50;
        [self addSubview:btn];
    }
    [kKeyWindow addSubview:self];
    self.frame = CGRectMake(0, KSCREENHEIGHT, KSCREENWIDTH, hei);
    [UIView animateWithDuration:0.25 animations:^{
        CGRect sF = self.frame;
        sF.origin.y = [UIScreen mainScreen].bounds.size.height - sF.size.height;
        self.frame = sF;
    }];
}

- (void)setFrame:(CGRect)frame
{
    frame.size.width = [UIScreen mainScreen].bounds.size.width;
    if (frame.size.height <= 0)
    {
        frame.size.height = 00;
    }
    frame.origin.x = 0;
    [super setFrame:frame];
}

#pragma mark - 添加背景视图
- (void)addBackView:(UIView *)superView
{
    _backView = [[UIView alloc] init];
    _backView.frame = superView.bounds;
    _backView.backgroundColor = [UIColor blackColor];
    _backView.alpha = 0.4;
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapBg)];
    [_backView addGestureRecognizer:tap];
    [superView addSubview:_backView];
}

#pragma mark 点击背景阴影视图触发的方法
- (void)tapBg
{
    [_backView removeFromSuperview];
    _backView = nil;
    [UIView animateWithDuration:0.25 animations:^{
        CGRect sf = self.frame;
        sf.origin.y = [UIScreen mainScreen].bounds.size.height;
        self.frame = sf;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

@end
